import getInvoiceType from '../../lib/getInvoiceType';
const basicIconNames = ['proven-true', 'verfiy-true', 'verfiy-result', 'close'];
const basicImageIconNames = ['zoomin', 'zoomout', 'rotate-left', 'rotate-right', 'download', 'fullscreen'];
const basicPdfIconNames = ['fullscreen', 'download', 'compatible', 'verfiy-true'];
const basicOfdIconNames = ['download', 'verfiy-true', 'preview'];
const basicXMLIconNames = ['download'];
/**
 * 
 * @param {*} allIcons 所有内部定义的icon信息，包含名称，css等信息
 * @param {*} activeInvoice 当前激活状态的invoice
 * @param {*} iconClickFns icon对应的方法集合
 * @param {*} defineTools 自定义的工具栏中的icons方法
 * @returns 
 */
function useSplitIcons(allIcons = [], activeInvoice, iconClickFns, defineTools) {
    let icons = [...allIcons];
    if (typeof defineTools === 'function') {
        //* 如果外部提供了自定义的方法,则通过外部方法过滤一遍
        let { lastIcons = [], newIcons = [] } = defineTools(icons, activeInvoice);
        // todo 以下代码没必要放到这里处理，只可在defineTools处理
        icons = lastIcons.filter(icon => {
            let matchedIndex = icons.findIndex(d => d.name === icon.name);
            return matchedIndex > -1
        });
        if (newIcons?.length > 0) {
            icons = [...icons, ...newIcons];
        }
    }
    const invoiceType = getInvoiceType(activeInvoice?.attachmentFiles?.[0] || {});
    let imageIcons = [...basicIconNames, ...basicImageIconNames];
    let pdfIcons = [...basicIconNames, ...basicPdfIconNames];
    let ofdIcons = [...basicIconNames, ...basicOfdIconNames];
    let XMLIcons = [...basicIconNames, ...basicXMLIconNames];
    let otherIcons = [...basicIconNames];    
    const leftIcons = [],
        rightIcons = [];
    icons.forEach(icon => {
        let cloneIcon = { ...icon };
        let filterIcons = [];
        if (invoiceType === 'img') {
            filterIcons = imageIcons;
        } else if (invoiceType === 'pdf') {
            filterIcons = pdfIcons;
        } else if (invoiceType === 'ofd') {
            filterIcons = ofdIcons;
        } else if (invoiceType === 'xml') {
            filterIcons = XMLIcons;
        } else {
            filterIcons = otherIcons;
        }
        /*NOTE 这里仅继承内部的onClick事件 */
        if (!icon.onClick && iconClickFns && typeof iconClickFns?.[cloneIcon.name]) {
            cloneIcon.onClick = iconClickFns?.[cloneIcon.name];
        }
        if (filterIcons.includes(cloneIcon.name)) {
            if (cloneIcon.direction === 'right') {
                rightIcons.push(cloneIcon);
            } else {
                leftIcons.push(cloneIcon);
            }
        }
    })
    return { leftIcons, rightIcons }
}
export default useSplitIcons;